package com.eastedu.report.common.util;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;

/**
 * The interface Render util.
 *
 * @author superman
 */
public interface FileUtil {
    /**
     * The constant SIMPLE_DATE_FORMAT_THREAD_LOCAL.
     */
    ThreadLocal<SimpleDateFormat> SIMPLE_DATE_FORMAT_THREAD_LOCAL = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    /**
     * Generate temp directory string.
     *
     * @param prefix 目录前缀
     * @param suffix 文件后缀名
     * @return the string
     */
    static String generateTempFile(String prefix, String suffix) {
        Path path = Paths.get(FileUtils.getTempDirectoryPath(), "reports", prefix);
        File file = path.toFile();
        if (!file.exists()) {
            file.mkdirs();
        }
        return Paths.get(path.toString(), suffix).toString();
    }

    /**
     * Delete temp file.
     *
     * @param prefix the prefix
     * @throws IOException the io exception
     */
    static void deleteTempFile(String prefix) throws IOException {
        FileUtils.deleteDirectory(Paths.get(FileUtils.getTempDirectoryPath(), "reports", prefix).toFile());
    }

    /**
     * Is word boolean.
     *
     * @param contentType the content type
     * @return the boolean
     */
    static Boolean isWord(String contentType) {
        return ("application/vnd.openxmlformats-officedocument.wordprocessingml.document".equalsIgnoreCase(contentType)
                || "application/msword".equalsIgnoreCase(contentType));
    }
}
